package com.jonniespratley.codegen.utils
{
	import mx.core.UIComponent;

	/**
	 * JSONUtilities
	 * @method format( String ) - Formats JSON and makes it look pretty.
	 * @author Jonnie
	 *
	 */
	public class JSONUtilities
	{
		public function JSONUtilities(){}

		/**
		[//new line
			{//newline + one tab
		        "id":"1",//newline + two tab
		        "map_lat":"955.000000",
		        "map_lon":"365.000000",
		        "map_type":"81",
		        "location_id":"86",
		        "user_id":"23"
		    },//newline + one tab
		    {
		        "id":"2",
		        "map_lat":"771.000000",
		        "map_lon":"534.000000",
		        "map_type":"89",
		        "location_id":"28",
		        "user_id":"25"
		    }
		]//newline
		*/
		static public function format( uglyJSON:String ):String
		{
			var endNameValuePattern:RegExp = /(\",)/gim;
			var openArrayBracePattern:RegExp = /[\[]/gim;
			var closeArrayBracePattern:RegExp = /[\]]/gim;
			var openObjectBracePattern:RegExp = /[{]/gim;
			var closeObjectBracePattern:RegExp = /(\},)/gim;

			var oneLine:String = '\n';
			var oneTab:String = '\n\t';
			var twoTab:String = '\n\t\t';

			var results:String = uglyJSON.replace( openArrayBracePattern, '\n[' + oneTab );
				results = results.replace( openObjectBracePattern,  '{' + twoTab );
				results = results.replace( endNameValuePattern, '",' + twoTab );
				results = results.replace( closeObjectBracePattern, oneTab + '},' + oneTab );
				results = results.replace( closeArrayBracePattern, oneLine + ']' );



			/*
			===============================================================
			Now for the coloring
			[ ] array brace = 0033FF
			{ } object brace = 00AA00 00AA00
			property name = CC0000
			word = 007777


			This below matches every thing that is not a {}[]:"
			/[^{}\[\],"\s:]/gm

			This below matches every thing that is a {}[]:"
			/[{}\[\],":]/gm

			private function findText( reg:String, text:String ):void
		{
			var pattern:RegExp = new RegExp( reg, 'gi' );
			var str:String = text;
			var result:Array = pattern.exec( str );
			var range:TextRange;

			while ( result != null )
			{
				var start:int = result.index;
				var end:int = pattern.lastIndex;

				range = new TextRange( txt_target, false, start, end );
				range.color = 0xff0000;

				_editorOutput +=
							"\nIndex: " + result.index +
							"\nLast Index: " + pattern.lastIndex +
							"\nResult: " + result + "\n";

				result = pattern.exec( str );
			}
			highlightComments( text );

			//dg_matches.dataProvider = new ArrayCollection( result );
		}

			var patternArrayBrace:RegExp = /[\[\]]/gm;
			var patternObjectBrace:RegExp = /[\{\}]/gm;
			var patternNotString:RegExp = /[{}\[\],":]/gm;

			var patternString:RegExp = /[^{}\[\],"\s:]/gm;
			var colorArrayBrace:Number = 0x0033FF;
			var colorObjectBrace:Number = 0x00AA00;
			var colorProperty:Number = 0xCC0000;
			var colorString:Number = 0x007777;
			var range:TextRange;

			var matchedArray:Array = patternString.exec( results );

			while( matchedArray != null )
			{
				var start:int = matchedArray.index;
				var end:int = patternString.lastIndex;

				range = new TextRange( txt_responseRaw, false, start, end );
				range.color = colorString;
				range.fontWeight = 'bold';

				matchedArray = patternString.exec( results );
			}
			===============================================================
			*/

			return results;
		}
	}
}